home *** CD-ROM | disk | FTP | other *** search
- Path: news.ov.com!news
- From: glenn@ov.com (Fletcher.Glenn@ov.com)
- Newsgroups: comp.lang.c
- Subject: Re: HELPP!!! --very important-- #2
- Date: 22 Mar 1996 22:35:22 GMT
- Organization: OpenVision
- Message-ID: <4iv9va$95t@spanky.pls.ov.com>
- References: <1996Mar21.001307.138164@forest>
- Reply-To: glenn@ov.com
- NNTP-Posting-Host: foghorn.pls.ov.com
-
- Below is your program properly formatted. Your error is to be found
- on the lines flagged with /****Added by FMG***/. Basically your
- routine works fine when you remember to keep functions together
- with braces.
-
- Fletcher.Glenn@ov.com
-
- #include <stdio.h>
- #include <math.h>
- #define NMAX 1000
-
- int
- main(void)
- {
-
- int n = 1, min_div, /* minimum divisor (greater than 1) of n */
- step = 1;
- printf("%i ", n); /* prints out 1.. thew first prime number */
- for (n = 2; n <= NMAX; n++)
- {
- min_div = find_div(n);
- if (min_div == n)
- printf("%i ", n);
- step++;
- if (step == 20)
- printf("\n");
- step = 0; /* end of if min_div == n */
- /* end for loop */
- }
- printf("\n"); /* nesisary carriage return (sorry about my
- * spelling) */
- /* end main */
- }
- int
- find_div(int n)
- {
- int trial, /* current value of n */
- divisor; /* smallest divisor of n; zero means that
- * divisor not yet found */
- int even(int num);
-
- if (even(n))
- divisor = 2;
- else
- { /******added by FMG *****/
- divisor = 0;
- trial = 3;
- } /******added by FMG *****/
-
- while (divisor == 0) /* finds is there is any possible int that
- * would kepp it from being a prime number */
- if (trial > sqrt(n))
- divisor = n;
- else if ((n % trial) == 0)
- divisor = trial;
- else
- trial = trial + 2;
-
-
- return (divisor);
- /* end find_div */
- }
-
- int
- even(int num) /* finds is num is even or not */
- {
- int ans;
-
- ans = ((num % 2) == 0);
- return (ans); /* returns 1 if even, 0 if not */
-
- /* end even */
- }
-
-
-
-